100 Shell Programs in Unix by Sarika Jain

100 Shell Programs in Unix by Sarika Jain

Author:Sarika Jain
Language: eng
Format: epub, mobi


39. Write a shell script to generate first ‘n’ terms of the following sequence without using multiplication-1 2 4 8 16 32…………n.

$vi prg39

clear

echo enter the value of n

read n

echo

i=1

while test $i –le $n

do

echo $i

i=‘expr $i + $i’

done

Sample Run

$sh p39

enter the value of n

20

1

2

4

8

16

40. Write a shell script to find greatest common divisor (GCD) for two given numbers.

$vi prg40

clear

echo enter numbers a and b

read a b

while [ 1 ] # infinite loop

do

c=‘expr $a % $b’

if [ $c -eq 0 ]

then

echo GCD = $b

exit

fi

a=$b

b=$c

done

Sample Run

$sh prg40

enter numbers a and b 47 3 GCD = 1



Download



Copyright Disclaimer:
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.